package com.sangfor.ssl;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.line.DNSTask;
import com.sangfor.ssl.service.line.LineResult;
import com.sangfor.ssl.service.line.LineSelectManager;
import com.sangfor.ssl.service.line.LineSelectionTask;
import com.sangfor.ssl.service.line.SelectLineCallback;
import com.sangfor.ssl.service.netmonitor.NetworkMonitor;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.utils.IGeneral;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/* loaded from: classes2.dex */
public class LineSelectionModel {
    private static final String TAG = "LineSelectionModel";
    private Context mContext;
    private List<String> mHostAddress;

    /* loaded from: classes2.dex */
    public interface DNSResult {
        void afterDNSTask(boolean z);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static class SingletonHolder {
        private static final LineSelectionModel INSTANCE = new LineSelectionModel();

        private SingletonHolder() {
        }
    }

    public static final LineSelectionModel getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private LineSelectionModel() {
    }

    public void init(Context context) {
        this.mContext = context;
        NetworkMonitor.getInstance().startMonitor(context.getApplicationContext());
        NetworkChangeManager.getInstance().init();
    }

    public void postLineSelectTask(LineSelectionTask.TaskType taskType, SelectLineCallback selectLineCallback) {
        if (TextUtils.isEmpty("")) {
            Log.error(TAG, "input url is null, no need line selection");
        } else {
            postLineSelectTask("", taskType, selectLineCallback, null);
        }
    }

    public void postLineSelectTask(String str, LineSelectionTask.TaskType taskType, SelectLineCallback selectLineCallback, Handler handler) {
        Log.info(TAG, "start line select task, type:" + taskType.name() + "  input url:" + str);
        LineSelectManager.getInstance().doLineSelectTask(str, taskType, selectLineCallback, handler);
    }

    public void doCancelLineTask(LineSelectionTask.TaskType taskType) {
        Log.info(TAG, "cancel line select task, type:" + taskType.name());
        LineSelectManager.getInstance().doCancelLineTask(taskType);
    }

    public boolean saveSelectInfo(String str, LineResult lineResult) {
        if (TextUtils.isEmpty(str) || lineResult == null || TextUtils.isEmpty(lineResult.selectUrl) || TextUtils.isEmpty(lineResult.vpnAddress)) {
            Log.error(TAG, "Select line is empty");
            return false;
        }
        SettingManager settingManager = SettingManager.getInstance();
        try {
            String str2 = lineResult.vpnAddress;
            if (!str2.startsWith(IGeneral.PROTO_HTTPS_HEAD) && !str2.startsWith(IGeneral.PROTO_HTTP_HEAD)) {
                str2 = String.format(Locale.getDefault(), "https://%s", str2);
            }
            settingManager.setGlobalValue(SettingManager.KEY_L3VPN_HOST, new URL(str2).getHost());
            try {
                settingManager.setGlobalValue(SettingManager.TWFID_HOST, new URL((str.startsWith(IGeneral.PROTO_HTTPS_HEAD) || str.startsWith(IGeneral.PROTO_HTTP_HEAD)) ? str : String.format(Locale.getDefault(), "https://%s", str)).getHost());
                settingManager.setGlobalValue(SettingManager.GLOBAL_URL_KEY, str);
                settingManager.setGlobalValue(SettingManager.GLOBAL_HOST_DOMAIN_KEY, lineResult.selectUrl);
                settingManager.setGlobalValue(SettingManager.GLOBAL_HOST_KEY, lineResult.vpnAddress);
                settingManager.set(SettingManager.TWFID_REALLINE, lineResult.vpnAddress);
                Log.info(TAG, "inputUrl:" + str + "\nselectUrl:" + lineResult.selectUrl + "\nvpnAddress:" + lineResult.vpnAddress);
                return true;
            } catch (MalformedURLException e) {
                Log.error(TAG, "Malformed URL Error, ", e);
                return false;
            }
        } catch (MalformedURLException e2) {
            Log.error(TAG, "Malformed URL Error, ", e2);
            return false;
        }
    }

    public String getCandidates() {
        StringBuilder sb = new StringBuilder();
        Iterator<String> it = NetworkMonitor.getInstance().getCandidateHost().iterator();
        while (it.hasNext()) {
            String next = it.next();
            if (it.hasNext()) {
                sb.append(next);
                sb.append(";");
            } else {
                sb.append(next);
            }
        }
        return sb.toString();
    }

    private List<String> getHostCandidates() {
        return this.mHostAddress;
    }

    public void postDNSTask(final DNSResult dNSResult) {
        Set<String> candidateHost = NetworkMonitor.getInstance().getCandidateHost();
        if (candidateHost == null) {
            dNSResult.afterDNSTask(false);
        } else {
            new DNSTask(candidateHost, new DNSTask.DNSCallback() { // from class: com.sangfor.ssl.LineSelectionModel.1
                @Override // com.sangfor.ssl.service.line.DNSTask.DNSCallback
                public void doAfterDNSTask(boolean z, List<String> list) {
                    LineSelectionModel.this.mHostAddress = list;
                    dNSResult.afterDNSTask(z);
                }
            }).execute(new Void[0]);
        }
    }
}
